package Order;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class OrderWindow extends JFrame {
    private JPanel mainPanel;
    private JPanel topPanel;
    private JButton checkOutBtn;
    private JLabel titleLabel;
    private JPanel bottomPanel;
    private JTextField nameTextField;
    private JTextField phoneTextField;
    private JTextField addressTextField;
    private JLabel nameLabel;
    private JLabel phoneLabel;
    private JLabel addressLabel;
    private JLabel totalPriceLabel;
    private JTable itemsTable;
    private JLabel totalPriceShowArea;
    private JPanel informationPanel;
    private JPanel informationEditPanel;
    private JPanel itemsPanel;
    private JScrollPane itemsScrollPane;
    private JPanel totalPriceShowPanel;
    private JPanel totalPriceLabelPanel;
    private JPanel itemsLabelPanel;
    private JLabel itemsLabel;

    private final Dimension topPanelSize = new Dimension(430, 50);
    private final Dimension tablePanelSize = new Dimension(430, 180);
    private final Dimension leftLabelPanelSize = new Dimension(90, 50);
    private final Dimension rightLabelPanelSize = new Dimension(320, 50);
    private final Dimension leftInfoPanelSize = new Dimension(90, 150);
    private final Dimension rightInfoPanelSize = new Dimension(320, 150);
    private final Dimension bottomPanelSize = new Dimension(430, 50);

    public OrderWindow() {
//        setTheme();

        topPanel.setMaximumSize(topPanelSize);
        topPanel.setMinimumSize(topPanelSize);
        topPanel.setPreferredSize(topPanelSize);
        topPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        topPanel.setBackground(new Color(189, 74, 96));

        titleLabel.setMaximumSize(new Dimension(220, 30));
        titleLabel.setMinimumSize(new Dimension(220, 30));
        titleLabel.setPreferredSize(new Dimension(220, 30));
        titleLabel.setText("订单信息");
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setForeground(new Color(255, 255, 255));


        informationPanel.setLayout(new BoxLayout(informationPanel, BoxLayout.Y_AXIS));
        informationPanel.setMaximumSize(leftInfoPanelSize);
        informationPanel.setMinimumSize(leftInfoPanelSize);
        informationPanel.setPreferredSize(leftInfoPanelSize);
        informationPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        informationPanel.setBackground(new Color(236, 159, 178));


        nameLabel.setMaximumSize(leftLabelPanelSize);
        nameLabel.setMinimumSize(leftLabelPanelSize);
        nameLabel.setPreferredSize(leftLabelPanelSize);
        phoneLabel.setMaximumSize(leftLabelPanelSize);
        phoneLabel.setMinimumSize(leftLabelPanelSize);
        phoneLabel.setPreferredSize(leftLabelPanelSize);
        addressLabel.setMaximumSize(leftLabelPanelSize);
        addressLabel.setMinimumSize(leftLabelPanelSize);
        addressLabel.setPreferredSize(leftLabelPanelSize);

        nameLabel.setText("姓名：");
        phoneLabel.setText("手机号：");
        addressLabel.setText("地址：");

        nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        phoneLabel.setHorizontalAlignment(SwingConstants.CENTER);
        addressLabel.setHorizontalAlignment(SwingConstants.CENTER);


        informationEditPanel.setLayout(new BoxLayout(informationEditPanel, BoxLayout.Y_AXIS));
        informationEditPanel.setMaximumSize(rightInfoPanelSize);
        informationEditPanel.setMinimumSize(rightInfoPanelSize);
        informationEditPanel.setPreferredSize(rightInfoPanelSize);
        informationEditPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        informationEditPanel.setBackground(new Color(236, 159, 178));


        itemsLabelPanel.setMaximumSize(new Dimension(430, 50));
        itemsLabelPanel.setMinimumSize(new Dimension(430, 50));
        itemsLabelPanel.setPreferredSize(new Dimension(430, 50));
        itemsLabelPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        itemsLabelPanel.setBackground(new Color(189, 74, 96));

        itemsLabel.setForeground(new Color(255, 255, 255));


        itemsPanel.setMaximumSize(tablePanelSize);
        itemsPanel.setMinimumSize(tablePanelSize);
        itemsPanel.setPreferredSize(tablePanelSize);
        itemsPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));


        totalPriceLabelPanel.setMaximumSize(leftLabelPanelSize);
        totalPriceLabelPanel.setMinimumSize(leftLabelPanelSize);
        totalPriceLabelPanel.setPreferredSize(leftLabelPanelSize);
        totalPriceLabelPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        totalPriceLabelPanel.setBackground(new Color(236, 159, 178));


        totalPriceShowPanel.setMaximumSize(rightLabelPanelSize);
        totalPriceShowPanel.setMinimumSize(rightLabelPanelSize);
        totalPriceShowPanel.setPreferredSize(rightLabelPanelSize);
        totalPriceShowPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        totalPriceShowPanel.setBackground(new Color(236, 159, 178));


        bottomPanel.setMaximumSize(bottomPanelSize);
        bottomPanel.setMinimumSize(bottomPanelSize);
        bottomPanel.setPreferredSize(bottomPanelSize);

        checkOutBtn.setBackground(new Color(189, 74, 96));
        checkOutBtn.setForeground(new Color(255, 255, 255));

        checkOutBtn.addActionListener(e -> {
            System.out.println(this);
            int result = JOptionPane.showConfirmDialog(
                    this,
                    "确认提交订单？",
                    "提交订单",
                    JOptionPane.YES_NO_OPTION
            );
            if (result == JOptionPane.YES_OPTION) {
                // 提交订单逻辑
                JOptionPane.showMessageDialog(this, "订单提交成功！");
                OrderWindow.this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                OrderWindow.this.dispose();
            }
        });


    }

    public static void main(String[] args) {
//        设置Frame
        JFrame jFrame = new JFrame();
        jFrame.setTitle("订单");
        jFrame.setSize(new Dimension(450, 600));
        jFrame.setResizable(false); // 禁止调整大小
        //设置主窗口
        OrderWindow window = new OrderWindow();
        jFrame.setContentPane(window.mainPanel);
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //显示
        jFrame.setVisible(true);
    }

    public void setTable(String[][] data) {
        String[] columns = {"商家", "商品", "单价", "数量"};
        DefaultTableModel tableModel = new DefaultTableModel(data, columns);
        this.itemsTable.setModel(tableModel);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
        for (int i = 0; i < itemsTable.getColumnCount(); i++) {
            itemsTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
        }
        this.itemsScrollPane.setViewportView(itemsTable);
    }

    public void setTotalPrice(double price) {
        this.totalPriceShowArea.setText("￥" + String.valueOf(price));
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public static void setTheme() {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(6, 2, new Insets(0, 0, 0, 0), -1, -1));
        mainPanel.add(panel1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        topPanel = new JPanel();
        topPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(topPanel, new GridConstraints(0, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        titleLabel = new JLabel();
        titleLabel.setHorizontalAlignment(10);
        titleLabel.setText("订单信息");
        topPanel.add(titleLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        bottomPanel = new JPanel();
        bottomPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(bottomPanel, new GridConstraints(5, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        checkOutBtn = new JButton();
        checkOutBtn.setText("确认并提交");
        bottomPanel.add(checkOutBtn, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        informationPanel = new JPanel();
        informationPanel.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(informationPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        nameLabel = new JLabel();
        nameLabel.setText("姓名：");
        informationPanel.add(nameLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        phoneLabel = new JLabel();
        phoneLabel.setText("手机号：");
        informationPanel.add(phoneLabel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        addressLabel = new JLabel();
        addressLabel.setText("地址：");
        informationPanel.add(addressLabel, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        informationEditPanel = new JPanel();
        informationEditPanel.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(informationEditPanel, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        nameTextField = new JTextField();
        informationEditPanel.add(nameTextField, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        phoneTextField = new JTextField();
        informationEditPanel.add(phoneTextField, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        addressTextField = new JTextField();
        informationEditPanel.add(addressTextField, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        itemsPanel = new JPanel();
        itemsPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(itemsPanel, new GridConstraints(3, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        itemsScrollPane = new JScrollPane();
        itemsPanel.add(itemsScrollPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        itemsTable = new JTable();
        itemsScrollPane.setViewportView(itemsTable);
        totalPriceShowPanel = new JPanel();
        totalPriceShowPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(totalPriceShowPanel, new GridConstraints(4, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        totalPriceShowArea = new JLabel();
        totalPriceShowArea.setText("Label");
        totalPriceShowPanel.add(totalPriceShowArea, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        totalPriceLabelPanel = new JPanel();
        totalPriceLabelPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(totalPriceLabelPanel, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        totalPriceLabel = new JLabel();
        totalPriceLabel.setText("总价：");
        totalPriceLabelPanel.add(totalPriceLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        itemsLabelPanel = new JPanel();
        itemsLabelPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(itemsLabelPanel, new GridConstraints(2, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        itemsLabel = new JLabel();
        itemsLabel.setText("购买商品");
        itemsLabelPanel.add(itemsLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mainPanel;
    }
}
